package cn.com.xuhx.testPage;

import cn.com.xuhx.mapper.UserMapper;
import cn.com.xuhx.pojo.User;
import cn.com.xuhx.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.bytebuddy.build.ToStringPlugin;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 分页测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class PageTest {

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页测试（注意：IService中没有定制的分页查询函数，需要我们自己取写对应的方法，调用mapper层的selectPage）
     */
    @Test
    public void pageTest(){
        //SELECT COUNT(*) FROM T_USERS WHERE delStatus = 0 AND (age >= ?)
        //SELECT id,name,age,address,tel,sfzh,bornday,delStatus FROM T_USERS WHERE delStatus=0 AND (age >= ?) ORDER BY age DESC LIMIT ?,?
        Page<User> userPage = new Page<>(2, 3);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.ge("age",10).orderByDesc("age");
        userMapper.selectPage(userPage, wrapper);
        //获取查询数据
        System.out.println(userPage.getRecords());
        //获取总数
        System.out.println(userPage.getTotal());
    }

    /**
     * 自定义分页测试（用于自定义SQL的分页）
     */
    @Test
    public void definePageTest(){
        //select * from T_USERS where age >= ? LIMIT ?,?
        Page<User> userPage = new Page<>(2, 3);
        userMapper.selectPageDefine(userPage, 10);
        //获取查询数据
        System.out.println(userPage.getRecords());
        //获取总数
        System.out.println(userPage.getTotal());
    }
}
